package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.common.Result;
import com.example.domain.entity.LoginLog;
import com.example.domain.query.LoginLogQuery;
import com.example.service.LoginLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@Tag(name = "登录日志管理")
@RestController
@RequestMapping("/loginLog")
@RequiredArgsConstructor
public class LoginLogController {

    private final LoginLogService loginLogService;

    @Operation(summary = "获取登录日志列表")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:loginLog:query')")
    public Result<IPage<LoginLog>> list(LoginLogQuery query) {
        return Result.success(loginLogService.getLoginLogPage(query));
    }

    @Operation(summary = "批量删除登录日志")
    @DeleteMapping("/batch")
    @PreAuthorize("hasAuthority('sys:loginLog:delete')")
    public Result<?> batchDelete(@RequestBody List<Long> ids) {
        loginLogService.deleteLoginLogByIds(ids);
        return Result.success();
    }

    @Operation(summary = "清空登录日志")
    @DeleteMapping("/clear")
    @PreAuthorize("hasAuthority('sys:loginLog:delete')")
    public Result<?> clear() {
        loginLogService.clearLoginLog();
        return Result.success();
    }

    @Operation(summary = "导出登录日志")
    @PostMapping("/export")
    @PreAuthorize("hasAuthority('sys:loginLog:export')")
    public void export(@RequestBody LoginLogQuery query, HttpServletResponse response) {
        loginLogService.exportLoginLog(query, response);
    }
} 